plotly_express

plotly_express is a terse, consistent, high-level wrapper around plotly for rapid data exploration and figure generation.

This notebook demonstrates various plotly_express features. Reference documentation is also available.

In [1]:
import plotly_express as px

iris = px.data.iris()
tips = px.data.tips()
gapminder = px.data.gapminder()
election = px.data.election()
wind = px.data.wind()
carshare = px.data.carshare()
In [2]:
px.scatter(iris, x="sepal_width", y="sepal_length")
In [3]:
px.scatter(iris, x="sepal_width", y="sepal_length", color="species")
In [4]:
px.scatter(iris, x="sepal_width", y="sepal_length", color="species", marginal_y="violin", marginal_x="rug")
In [5]:
px.scatter(iris, x="sepal_width", y="sepal_length", color="species", marginal_y="histogram", 
           marginal_x="box", trendline="ols")
In [6]:
iris["e"] = iris["sepal_width"]/100
px.scatter(iris, x="sepal_width", y="sepal_length", color="species", error_x_minus="e")
In [7]:
del iris["e"]
In [8]:
px.scatter(tips, x="total_bill", y="tip", facet_row="time", facet_col="day", color="smoker", trendline="ols",
          category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
In [9]:
px.scatter_matrix(iris)
In [10]:
px.scatter_matrix(iris, dimensions=["sepal_width", "sepal_length", "petal_width", "petal_length"], 
         color="species", symbol="species")
In [11]:
px.parallel_coordinates(iris, color="species_id", labels={"species_id": "Species",
                  "sepal_width": "Sepal Width", "sepal_length": "Sepal Length", 
                  "petal_width": "Petal Width", "petal_length": "Petal Length", },
                    color_continuous_scale=px.colors.diverging.Tealrose, color_continuous_midpoint=2)
In [12]:
px.parallel_categories(tips, color="size", color_continuous_scale=px.colors.sequential.Inferno)
In [13]:
px.scatter(tips, x="total_bill", y="tip", color="size", facet_col="sex",
           color_continuous_scale=px.colors.sequential.Viridis, render_mode="webgl")
In [14]:
px.scatter(gapminder.query("year == 2007"), x="gdpPercap", y="lifeExp", size="pop", 
           color="continent", hover_name="country", log_x=True, size_max=60)
In [15]:
px.scatter(gapminder, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country", facet_col="continent",
           log_x=True, size_max=45, range_x=[100,100000], range_y=[25,90])
In [16]:
px.line(gapminder, x="year", y="lifeExp", color="continent", line_group="country", hover_name="country", 
        line_shape="spline")
In [17]:
px.density_contour(iris, x="sepal_width", y="sepal_length")
In [18]:
px.density_contour(iris, x="sepal_width", y="sepal_length", color="species")
In [19]:
px.density_contour(tips, x="total_bill", y="tip", color="day", facet_col="day")
In [20]:
px.box(tips, x="total_bill", y="day", orientation="h", points="all",
       category_orders={"day": ["Thur", "Fri", "Sat", "Sun"]})
In [21]:
px.bar(tips, x="total_bill", y="sex", orientation="h", color="smoker")
In [22]:
px.histogram(tips, x="total_bill", y="tip")
In [23]:
px.histogram(tips, x="sex", y="tip", color="smoker", facet_row="day", orientation="v", barmode="group",
      category_orders={"day": ["Thur", "Fri", "Sat", "Sun"]})
In [24]:
px.histogram(tips, x="total_bill", color="smoker", barmode="relative")
In [25]:
px.histogram(tips, x="total_bill", color="smoker", facet_row="day", facet_col="time")
In [26]:
px.violin(tips, y="tip", x="smoker", box=True, points="all")
In [27]:
px.violin(tips, y="tip", x="sex", color="smoker", facet_row="day")
In [28]:
px.violin(tips, y="tip", x="sex", color="smoker", facet_col="day", facet_row="time")
In [29]:
px.box(tips, y="tip", x="sex")
In [30]:
px.box(tips, y="tip", x="sex", color="smoker", facet_col="day", facet_row="time", 
       category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
In [31]:
px.box(tips, x="tip", y="sex", color="smoker", facet_col="day", facet_row="time", orientation="h")
In [32]:
px.scatter_ternary(election, a="Joly", b="Coderre", c="Bergeron", color="winner", size="total", hover_name="district",
                   symbol="result", 
                   size_max=15, color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"}
                  )
In [33]:
px.line_ternary(election, a="Joly", b="Coderre", c="Bergeron", color="winner", line_dash="winner")
In [34]:
px.scatter_3d(election, x="Joly", y="Coderre", z="Bergeron", color="winner", size="total", hover_name="district",
                  symbol="result", color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"})
In [35]:
px.line_3d(election, x="Joly", y="Coderre", z="Bergeron", color="winner", line_dash="winner")
In [36]:
px.scatter_polar(wind, r="value", theta="direction", color="strength", symbol="strength",
            color_discrete_sequence=px.colors.sequential.Plotly[-2::-1])
In [37]:
px.line_polar(wind, r="value", theta="direction", color="strength", line_close=True,
            color_discrete_sequence=px.colors.sequential.Plotly[-2::-1])
In [38]:
px.bar_polar(wind, r="value", theta="direction", color="strength", template="plotly_dark",
            color_discrete_sequence= px.colors.sequential.Plotly[-2::-1])
In [39]:
px.set_mapbox_access_token(open(".mapbox_token").read())
px.scatter_mapbox(carshare, lat="centroid_lat", lon="centroid_lon", color="peak_hour", size="car_hours", 
                  color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10)
In [40]:
px.set_mapbox_access_token(open(".mapbox_token").read())
px.line_mapbox(carshare, lat="centroid_lat", lon="centroid_lon", color="peak_hour")
In [41]:
px.scatter_geo(gapminder, locations="iso_alpha", color="continent", hover_name="country", size="pop", 
               animation_frame="year", projection="natural earth")
In [42]:
px.line_geo(gapminder.query("year==2007"), locations="iso_alpha", color="continent")
In [43]:
px.choropleth(gapminder, locations="iso_alpha", color="lifeExp", hover_name="country", animation_frame="year",
             color_continuous_scale=px.colors.sequential.Plasma)
In [44]:
px.colors.qualitative.swatches()
In [45]:
px.colors.sequential.swatches()
In [46]:
px.colors.diverging.swatches()
In [47]:
px.colors.cyclical.swatches()
In [48]:
px.colors.colorbrewer.swatches()
In [49]:
px.colors.cmocean.swatches()
In [50]:
px.colors.carto.swatches()